from src.game.ai.state import State
from pymunk.vec2d import Vec2d
from src.helper.movement import MovementHelper
import random

class WanderAI(State):
    
    def __init__(self, priority, entity, radius):
        State.__init__(self, priority, entity)
        self.radius = radius
        
    def shouldStart(self):
        return True
    
    def shouldContinue(self):
        return True
    
    def onStart(self):
        if bool(random.getrandbits(1)):
            x = random.uniform(0., self.radius)
        else:
            x = -random.uniform(0., self.radius)
        if bool(random.getrandbits(1)):
            y = random.uniform(0., self.radius)
        else:
            y = -random.uniform(0., self.radius)
        self.target = Vec2d(x, y)
        
    def update(self):
        if random.randint(0, 100) < 1:
            self.running = False
        self.entity.moveTo(self.entity.x + self.target.x, self.entity.y  + self.target.y)
        if MovementHelper.distanceToPoint((self.entity.x, self.entity.y), (self.entity.x  + self.target.x, self.entity.y + self.target.y)) < 2.:
            self.running = False
            self.target = None